﻿package org.hyy.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.lang.Integer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hyy.dao.AdminDAO;
import org.hyy.dao.CategoryDAO;
import org.hyy.dao.ProductDAO;
import org.hyy.vo.Category;
import org.hyy.vo.Product;

public class AdminMenu extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//接收参数
		String a = request.getParameter("menuid");
		int menuid = Integer.parseInt(a);
		//声明变量
		AdminDAO adminDAO = null;
		CategoryDAO categoryDAO = null; 
		List<Category> users = null;
		List<Category> categories = null;
		ProductDAO productDAO = null;
		List<Product> products = null;
		
		//判断用户点击了哪一个菜单
		switch (menuid) {
		case 1:
			adminDAO = new AdminDAO();
			users = new ArrayList();
			users = adminDAO.showall();
			//返回数据
			request.setAttribute("users", users);
			request.getRequestDispatcher("admin/showAll.jsp").forward(request, response);
			break;
		case 2:
			categoryDAO = new CategoryDAO();
			categories = new ArrayList<Category>();
			categories = categoryDAO.GetCatergories();
			
			request.setAttribute("categories", categories);
			request.getRequestDispatcher("admin/showCategory.jsp").forward(request, response);
			break;
		case 3:
			productDAO = new ProductDAO();
			products = new ArrayList<Product>();
			int totalRecords = productDAO.getRecoder(products, 1, 2, false);
			int totalPages = (totalRecords + 2 - 1) / 2;
			System.out.print(products.get(0).getName());
			request.setAttribute("products", products);
			request.getRequestDispatcher("admin/productList.jsp").forward(request, response);
			break;

		default:
			break;
		}
		
	}
	
	

}
